package tw.rescue.struts.action.guestbook;

import java.io.IOException;

import tw.rescue.biz.MessageBoardBiz;

public class GuestBookAction {

	MessageBoardBiz biz;
	
	public void setBiz(MessageBoardBiz biz) {
		this.biz = biz;
	}

	/**
	 * 顯示留言表單
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String guestbook_form() {
		return "form";
	}

	/**
	 * 建立新留言 thread
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public String guestbook_new() {
//		String nick = form.getNick();
//		String email = form.getEmail();
//		String subject = form.getSubject();
//		String content = form.getContent();
//		GuestBookMessage msg = new GuestBookMessage();
//		msg.setNick(nick);
//		msg.setEmail(email);
//		msg.setSubject(subject);
//		msg.setContent(content);
//		msg.setAllowReply(true);
//		msg.setRoot(true);
//		msg.setTop(false);
//		biz.newMessage(msg);
//		return jsonResponse(JSON_OK, biz.keyToString(msg.getKey()), null, 0);
		return "success";
	}

	/**
	 * 回覆留言
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public String guestbook_reply() {
//		String replyToKey = form.getReplyTo();
//		String nick = form.getNick();
//		String email = form.getEmail();
//		String subject = form.getSubject();
//		String content = form.getContent();
//		GuestBookMessage msg = new GuestBookMessage();
//		msg.setNick(nick);
//		msg.setEmail(email);
//		msg.setSubject(subject);
//		msg.setContent(content);
//		msg.setAllowReply(biz.isAllowReply(replyToKey));
//		msg.setRoot(false);
//		msg.setTop(false);
//		biz.replyMessage(replyToKey, msg);
//		return jsonResponse(JSON_OK, biz.keyToString(msg.getKey()), null, 0);
		return null;
	}

	/**
	 * 逐頁查詢留言
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String guestbook_list_message() {
//		String category = form.getCategory();
//		Integer from = form.getFrom();
//		Integer count = form.getCount();
//		if (from==null)
//			from = 0;
//		if (count==null)
//			count = 5;
//		List<GuestBookMessage>list = biz.getPage(category, from, count+1);
//		request.setAttribute("previous", Math.max(0, from-count));
//		request.setAttribute("from", from);
//		request.setAttribute("count", count);
//		request.setAttribute("hasNextPage", list.size()>count);
//		request.setAttribute("messageList", list.subList(0, Math.min(list.size(), count)));
		return "list";
	}
	
	public String guestbook_view_message() {
//		String mid = form.getMid();
//		GuestBookMessage message = biz.getMessage(mid);
//		request.setAttribute("message", message);
		return "view";
	}
}
